<?php

namespace app\index\model;
use think\Db;
use think\Model;
class Login extends Model
{
    function  login(){
      $input=input();//助手函数
     if(!$this->validate(true)->validateData($input)){
         throw new \Exception($this->getError());
     }
       $dbdata=$this->getByLogin($input['login']);
     if(empty($dbdata)){
         throw new \Exception('账号不存在');
     }
     if($dbdata['password']!=$input['password']){
         throw new \Exception('密码错误');
     }
//    print_r($dbdata['id']);
//        die;
     return User::get([$dbdata['id']]);
    }
    function getAccessUrls($roleId){
        $rows=Db::query('SELECT path FROM useable JOIN access ON useable.id=access.useable_id WHERE role_id=?',[$roleId]);
        $urls=[[]];
        foreach ($rows as $row){
            $urls[$row['path']]=true;
        }
        return $urls;
    }
    function getMenuData($roleId){
        $rows=Db::query('SELECT id,title,path,pid FROM useable JOIN access ON useable.id=access.useable_id WHERE role_id=?  AND visible=\'yes\'',[$roleId]);
        $tmps=[];
        foreach ($rows as &$row){
            $tmps[$row['id']]=&$row;//不懂
            unset($row['id']);//不需要id了。 unset设置
        }
        //再变立一下
        foreach ($rows as &$row){
            $tmps[$row['pid']]['children'][]=&$row;//children子目录
            unset($row['pid']);//不需要id了。 unset设置
        }
    return $tmps[0]['children'];
    }
}
